const crypto = require('crypto')//MD5加密
const jwt = require('jsonwebtoken')
const { PRIVATE_KEY } = require('./constant')

function isObject(o){
  return Object.prototype.toString.call(o) === '[object Object]'
}


function md5(s) {
// 注意参数需要为 String 类型，否则会出错
return crypto.createHash('md5')
    .update(String(s)).digest('hex');
}

function decoded(req) {//解析前端传过来的token
    const authorization = req.get('Authorization')
    var  token = ''
    if (authorization.indexOf('Bearer') >= 0) {
      token = authorization.replace('Bearer ', '')
    } else {
      token = authorization
    }
    return token=='Bearer'?false:jwt.verify(token, PRIVATE_KEY)
  }




  function checkMobile(s) { 
    var regu = /^[1][0-9][0-9]{9}$/; 
    var re = new RegExp(regu); 
    if (re.test(s)) { 
    return true; 
    } else { 
    return false; 
    } 
  } 


module.exports = {
    decoded,
    md5,
    isObject,
    checkMobile
}
